package org.xx.armory.commons;

import java.io.UnsupportedEncodingException;

import static java.net.URLEncoder.encode;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;

public final class ResponseUtils {
    private static final String RFC2231_ENCODING = "utf-8";

    private ResponseUtils() {
        throw new AssertionError();
    }

    /**
     * 根据扩展名获取内容类型。
     *
     * @param ext
     *         文件扩展名，应当以点({@literal .})开头
     * @return 内容类型，如果是无法识别的内容类型则返回{@literal application/octet-stream}。
     */
    public static String contentType(
            String ext
    ) {
        ext = trimToEmpty(ext).toLowerCase();
        return switch (ext) {
            case ".jpg", ".jpeg" -> "image/jpeg";
            case ".png" -> "image/png";
            case ".bmp" -> "image/bmp";
            case ".gif" -> "image/gif";
            case ".ico" -> "image/icon";
            case ".txt", ".text" -> "text/plain";
            case ".zip" -> "application/zip";
            case ".rar" -> "application/x-rar";
            case ".tar" -> "application/x-tar";
            case ".gz" -> "application/x-gzip";
            case ".doc" -> "application/vnc.ms-word";
            case ".docx" -> "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            case ".xls" -> "application/vnd.ms-excel";
            case ".xlsx" -> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            case ".ppt" -> "application/vnd.ms-powerpoint";
            case ".pptx" -> "application/vnd.openxmlformats-officedocument.presentationml.presentation";
            case ".pdf" -> "application/pdf";
            case ".sql" -> "text/x-sql";
            case ".ftl" -> "text/x-template";
            case ".html", ".htm" -> "text/html";
            default -> "application/octet-stream";
        };
    }

    /**
     * 按照RFC2231协议，使用UTF-8编码对文件名进行编码。
     *
     * @param fileName
     *         待编码的文件名，自动去掉首尾空格。
     * @return 编码后的结果。
     * @throws UnsupportedEncodingException
     *         如果运行环境中无法使用{@literal UTF-8}编码。
     * @throws IllegalArgumentException
     *         如果参数{@code fileName}是{@code null}或者只包含空白字符。
     */
    public static String encodeRFC2231(
            String fileName
    )
            throws UnsupportedEncodingException {
        fileName = trimToEmpty(fileName);

        if (fileName.isEmpty()) {
            throw new IllegalArgumentException("argument \"fileName\" cannot be null or empty!");
        }

        final StringBuilder result = new StringBuilder();

        result.append('"');
        result.append(RFC2231_ENCODING);
        result.append("''");
        final String encodedFileName = encode(fileName, RFC2231_ENCODING);
        for (int i = 0; i < encodedFileName.length(); ++i) {
            final char ch = encodedFileName.charAt(i);
            if (ch == '+') {
                result.append("%20");
            } else {
                result.append(ch);
            }
        }
        result.append('"');

        return result.toString();
    }


}
